<template>
  <div class="app-container">
    <!-- 表单 -->
    <advert-form @findList="findList" @addDialog="addDialog"  ref="formInlineRef" />

    <!-- 表格 -->
    <advert-table
      :tableData="tableData"
      :options="options"
      @delItem="delItem"
      @editDialog="editDialog"
    />

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增和编辑 弹出框 -->
    <advert-dialog
      ref="ruleFormRef"
      :RuleForm="ruleForm"
      :dialogVisible="dialogVisible"
      :dialogTitle="dialogTitle"
      :options="options"
      @addItem="addItem"
      @editItem="editItem"
    />
  </div>
</template>

<script>
import api from "@/api/advert";
import AdvertForm from "./AdvertForm";
import AdvertTable from "./AdvertTable";
import AdvertDialog from "./AdvertDialog";

export default {
  components: {
    AdvertForm,
    AdvertTable,
    AdvertDialog
  },
  data() {
    return {
      options: [
        { val: 0, title: "禁用" },
        { val: 1, title: "正常" }
      ],
      tableData: [],
      current: 1,
      size: 20,
      total: 0,
      dialogTitle: "",
      dialogVisible: false,
      ruleForm: {
        name: "",
        remark: "",
        sort: 0,
        status: ""
      }
    };
  },
  methods: {
    // 获取列表数据
    getList(e) {
      api.getList(e, this.current, this.size).then(response => {
        if (response.code == 20000) {
          this.tableData = response.data.records;
          this.total = response.data.total;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getList();
    },
    // 查询
    findList(e) {
      this.getList(e);
    },
    // 新增 dialog
    addDialog(e) {
      this.dialogVisible = e;
      this.dialogTitle = "新增";
      this.$refs.ruleFormRef.beforeResetForm();
    },
    // 新增 提交
    addItem(e) {
      api
        .addItem(e)
        .then(response => {
          if (response.code == 20000) {
            this.dialogVisible = false;
            this.$message.success(response.message);
            this.getList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 编辑 dialog
    editDialog(id) {
      this.dialogVisible = true;
      this.dialogTitle = "编辑";
      api.findItem(id).then(response => {
        if (response.code == 20000) {
          this.ruleForm = response.data;
          this.$refs.ruleFormRef.changeRuleForm(this.ruleForm);
        }
      });
    },
    // 新增 编辑 提交
    editItem(e) {
      api
        .editItem(e)
        .then(response => {
          if (response.code == 20000) {
            this.dialogVisible = false;
            this.$message.success(response.message);
            this.getList();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 删除
    delItem(e) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api.delItem(e).then(response => {
            if (response.code == 20000) {
              this.$message.success(response.message);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {
    this.getList();
  }
};
</script>
